package com.android.fragmentdemo.util
import android.text.TextUtils

object StringUtil {

    private const val PLACEHOLDER = "&"

    fun getNewStrWithInsertPlaceHolder(content: String, index: Int): String {
        val builder = StringBuilder()
        val condition = TextUtils.isEmpty(content)
        if (condition) {
            builder.append(PLACEHOLDER)
        } else {
            val contentArr = content.toCharArray()
            val length = contentArr.size
            if (index <= 0) {
                builder.append(PLACEHOLDER).append(content)
            } else if (index >= length - 1) {
                builder.append(content).append(PLACEHOLDER)
            } else {
                for (i in 0.until(index)) {
                    builder.append(contentArr[i])
                }
                builder.append(PLACEHOLDER)
                for (j in index.until(length)) {
                    builder.append(contentArr[j])
                }
            }
        }
        return builder.toString()
    }
}